<template>
	<view class="page">
		<view v-if="type == 1">
			<view class="text">当前真实姓名：{{userInfo.realname}}</view>
			<view class="u-input">
				<input type="text" placeholder="请输入修改后的真实姓名" v-model="realname">
			</view>
		</view>
		<view v-if="type == 2">
			<view class="text">当前昵称：{{userInfo.nickname}}</view>
			<view class="u-input">
				<input type="text" placeholder="请输入修改后的昵称" v-model="nickname">
			</view>
			<view class="text">性别：{{gender_text}}</view>
			<view class="u-input">
				<input type="text" placeholder="请选择性别" disabled="" @click='change_gender'>
			</view>
			<view class="text">身高(cm)：</view>
			<view class="u-input">
				<input type="number" placeholder="请输入身高" maxlength="6" v-model="m_height">
			</view>
			<view class="text">体重(kg)：</view>
			<view class="u-input">
				<input type="number" placeholder="请输入体重" maxlength="3" v-model="m_weight">
			</view>
			<view class="text">请选择出生年月日：</view>
			<view class="u-input">
				<picker class="picker" mode="date" :value="birthday" :start="startDate" :end="endDate" @change="bindDateChange">
					<view class="uni-input">{{birthday}}</view>
				</picker>
			</view>
		</view>
		
		
		<button class="u-confirm" @tap="confirm">提交</button>
	</view>
</template>

<script>
	import henglang from '@/common/common.js';
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
                realname: '',
				nickname: '',
				m_height: '',
				m_weight: '',
				type: 0,
				userInfo:{},
				data:{
					realname:'',
					nickname:'',
					m_height: '',
					m_weight: '',
					gender: ''
				},
				birthday: currentDate,
				gender_text: ''
			};
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
        methods:{
			change_gender() {
				let that = this;
				let gender_arr = ['保密', '男', '女'];
				uni.showActionSheet({
				    itemList: ['保密', '男', '女'],
				    success: function (res) {
				        that.data.gender = res.tapIndex;
						that.gender_text = gender_arr[res.tapIndex]
				    },
				    fail: function (res) {
				    }
				});
			},
            confirm() {
				let that = this
				if(that.type == 1 && that.realname){
					that.data.realname = that.realname
				}else if(that.type == 2 && that.nickname && that.m_height && that.m_weight){
					that.data.nickname = that.nickname
					that.data.birthday = that.birthday
					that.data.m_height = that.m_height
					that.data.m_weight = that.m_weight
				}else{
					henglang.showToast('请输入正确的基本信息')
					return false
				}
				henglang.post('member/updateMemberInfo',that.data,true,function(res){
					if(res.data.code == '0'){
						henglang.showToast('修改成功',500)
						uni.setStorage({
							key: 'userInfo',
							data: res.data.data
						});
						setTimeout(()=>{
							uni.navigateBack()
						},500)
					}else{
						henglang.showToast(res.data.msg)
					}
				})
            },
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
	
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			bindDateChange: function(e) {
				this.birthday = e.target.value
			}
        },
		onLoad({type}) {
			let that = this
			that.type = type
			henglang.get('member/getInfo',{},true,function(res){
				that.userInfo = res.data.data;
				if(that.userInfo.birthday != 0) that.birthday = that.userInfo.birthday
				if(that.userInfo.m_weight != 0) that.m_weight = that.userInfo.m_weight
				if(that.userInfo.m_height != 0) that.m_height = that.userInfo.m_height
				that.gender_text = that.userInfo.gender_text
			})
		
		}
	}
</script>

<style>
    .page {
        background: #f5f5f5;
    }
    .u-input {
        background: #fff;
        padding: 0 10px;
		height: 96upx;
		margin-bottom: 2upx;
		display: flex;
		align-items: center;
    }
	.u-input input{
		width: 100%;
		height: 36upx;
	}
	.u-input .picker{
		width: 100%;
	}
	.u-confirm{
		width: 90%;
		margin: 60upx 0 0 5%;
		border-radius: 100px;
		background: #d71311;
		color: #fff;
		text-align: center;
		padding: 30upx;
		font-size: 38upx;
	}
	.text{
		padding: 20upx;
	}
	.red{
		color: #D71311;
		padding-right: 10rpx;
	}
</style>
